package com.muchfish.dao;

import com.muchfish.io.Resources;
import com.muchfish.pojo.User;
import com.muchfish.sqlsession.SqlSession;
import com.muchfish.sqlsession.SqlSessionFactory;
import com.muchfish.sqlsession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements IUserDao{
    @Override
    public List<User> findAll() throws Exception {
        InputStream resourceAsSteam = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取当前方法的方法名和当前包路径
        return sqlSession.selectList("com.muchfish.dao.IUserDao.findAll");
    }

    @Override
    public User findByCondition(User user) throws Exception {
        InputStream resourceAsSteam = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession.selectOne("com.muchfish.dao.IUserDao.findByCondition", user);
    }
}
